package com.CST.service.impl;

import com.CST.common.api.Response;
import com.CST.dto.InheritDTO;
import com.CST.entity.EtlAcSet;
import com.CST.entity.RdCostsalaryrelation;
import com.CST.mapper.cst.EtlAcSetMapper;
import com.CST.service.IEtlAcSetService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xnd
 * @since 2022-01-12
 */
@Service
public class EtlAcSetServiceImpl extends ServiceImpl<EtlAcSetMapper, EtlAcSet> implements IEtlAcSetService {


    @Autowired
    private EtlAcSetMapper etlAcSetMapper;

    @Autowired
    private RdCostsalaryrelationServiceImpl rdCostsalaryrelationService;

    @Override
    public List<EtlAcSet> getAcSetSource(EtlAcSet entity) {
        return etlAcSetMapper.getAcSetSource(entity);
    }

    @Override
    public boolean inherit(InheritDTO dto) {
        RdCostsalaryrelation build = RdCostsalaryrelation.builder().ruleCode(dto.getRuleCodeOld())
                .yearMonth(dto.getYearMonthOld()).build();
        List<RdCostsalaryrelation> oldList = rdCostsalaryrelationService.getRdCostSalaryRelation(build);
        String[] split = dto.getYearMonthNew().split("-");
        String year = split[0];
        String month = split[split.length - 1];
        int monthNew = Integer.parseInt(month);
        int yearNew = Integer.parseInt(year);
        oldList.forEach(e -> {
            e.setRuleCode(dto.getRuleCodeNew());
            e.setYearMonth(dto.getYearMonthNew());
            e.setExtendField3(dto.getRuleNameNew());
            e.setExtendField1(String.valueOf(yearNew));
            e.setExtendField2(String.valueOf(monthNew));
        });
        rdCostsalaryrelationService.deleteByYearMonthAndRule(dto.getYearMonthNew(), dto.getRuleCodeNew());
        Response response = rdCostsalaryrelationService.insertBatchRdCostSalaryRelation(oldList);
        return "批量添加成功".equals(response.getMessage());
    }
}
